package itheima.com.myreflect4;

import java.lang.reflect.Field;

public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException {
//        method1();
        Class clazz = Class.forName("itheima.com.myreflect4.Student");
        Field money = clazz.getDeclaredField("money");
        money.setAccessible(true);
        Student student = (Student) clazz.newInstance();
        Object o = money.get(student);
        System.out.println(o);

    }

    private static void method1() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        Class clazz = Class.forName("itheima.com.myreflect4.Student");
        Field name = clazz.getField("name");
        //这样能获得构造方法
        Student student = (Student) clazz.newInstance();
        name.set(student,"rencai");
        System.out.println(student);
    }
}
